﻿using System.Collections.Generic;
using LxCore;

namespace LxCore
{
    public class VisualCenter : JsObject
    {
        public GraphList Graphs { get; set; } = new();
        public Graph CreateGraph(string name = null)
        {
            Graph graph = new();
            if (!string.IsNullOrEmpty(name)) { graph.Name = name; }
            Graphs.Add(graph);
            return graph;
        }
        public void RemoveGraphAt(int id)
        {
            Graphs.RemoveAt(id);
        }
        public void ClearHistory()
        {

        }
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(Graphs): jsReader.ReadList(Graphs, t => new Graph()); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            jsWriter.WriteList(nameof(Graphs), Graphs);
        }
        public class GraphList : JsIdentifyList<Graph>
        {
            public override void Add(Graph item)
            {
                base.Add(item);
                foreach (var z in LxSim.Sim.Conditions.ZoneConds)
                {
                    z.InnerCond.GraphSelector.Add(false);
                    foreach (var f in z.BoundConds)
                    {
                        f.GraphSelector.Add(false);
                    }
                }
            }
            public override void Insert(int index, Graph item)
            {
                base.Insert(index, item);
                foreach (var z in LxSim.Sim.Conditions.ZoneConds)
                {
                    z.InnerCond.GraphSelector.Insert(index, false);
                    foreach (var f in z.BoundConds)
                    {
                        f.GraphSelector.Insert(index, false);
                    }
                }
            }
            public override bool Remove(Graph item)
            {
                int index = item.ID;
                if (base.Remove(item))
                {
                    foreach (var z in LxSim.Sim.Conditions.ZoneConds)
                    {
                        z.InnerCond.GraphSelector.RemoveAt(index);
                        foreach (var f in z.BoundConds)
                        {
                            f.GraphSelector.RemoveAt(index);
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public override void RemoveAt(int index)
            {
                base.RemoveAt(index);
                foreach (var z in LxSim.Sim.Conditions.ZoneConds)
                {
                    z.InnerCond.GraphSelector.RemoveAt(index);
                    foreach (var f in z.BoundConds)
                    {
                        f.GraphSelector.RemoveAt(index);
                    }
                }
            }
            public override void Clear()
            {
                base.Clear();
                foreach (var z in LxSim.Sim.Conditions.ZoneConds)
                {
                    z.InnerCond.GraphSelector.Clear();
                    foreach (var f in z.BoundConds)
                    {
                        f.GraphSelector.Clear();
                    }
                }
            }
        }
    }
}
